package com.dmjj.common.util;

import com.dmjj.common.base.LoginEntity;

import java.io.Serializable;

/**
 * @author 王丽凯
 * @Description: 扫描二维码工具
 * @Title: ScanPool
 * @ProjectName dmjj-bigdata
 * @date 2019/3/15
 */
public class ScanPool implements Serializable {

    private static final long serialVersionUID = -9117921544228636690L;

    private final long waitTime = 5 * 1000L;

    private LoginEntity loginEntity;


    /**
     * @Description: 创建时间
     * @author 王丽凯
     * @date 2019/3/15
     */
    private Long createTime = System.currentTimeMillis();

    /**
     * @Description: 是否扫码状态
     * @author 王丽凯
     * @date 2019/3/15
     */
    private boolean scanFlag = false;

    /**
     * @Description: 是否已经扫描
     * @author 王丽凯
     * @date 2019/3/15
     */
    public boolean isScan(){
        return scanFlag;
    }

    /**
     * @Description: 修改扫描状态
     * @author 王丽凯
     * @date 2019/3/15
     */
    private void setScan(boolean scanFlag){
        this.scanFlag = scanFlag;
    }

    /**
     * @Description: 获取扫描状态如果未扫描等待 waitTime
     * @author 王丽凯
     * @date 2019/3/15
     */
    public synchronized boolean getScanStatus(){
        try {
            if (!isScan()){
                //未扫描等待三秒
                this.wait(waitTime);
            }
            if (isScan()){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @Description: 扫码之后设置扫码状态
     * @author 王丽凯
     * @date 2019/3/15
     */
    public synchronized void scanSuccess(){
        try {
            setScan(true);
            //释放资源继续执行
            this.notifyAll();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Description: 释放资源
     * @author 王丽凯
     * @date 2019/3/15
     */
    public synchronized void  notifyPool(){
        try {
            this.notifyAll();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public Long getCreateTime(){
        return createTime;
    }

    public LoginEntity getLoginEntity(){
        return this.loginEntity;
    }

    public void setLoginEntity(LoginEntity loginEntity){
        this.loginEntity = loginEntity;
    }
}
